/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knezevic.realestate.hibernate.spring;

import com.knezevic.realestate.RealEstate;
import com.knezevic.realestate.beans.RealEstateBean;
import com.knezevic.realestate.service.RealEstates;
import java.io.Serializable;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Nemanja
 */
public class RealEstatesDAO implements RealEstates{
    private SessionFactory sessionFactory;

     public SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Transactional
    public RealEstate save(RealEstate realEstate) {
        Long id = (Long) sessionFactory.getCurrentSession().save(realEstate);
        return (RealEstate) sessionFactory.getCurrentSession().get(RealEstateBean.class, id);
    }

    @Transactional
    public void update(RealEstate realEstate) {
        sessionFactory.getCurrentSession().merge(realEstate);
    }

    @Transactional
    public void deleteRealEstate(RealEstate realEstate) {
        sessionFactory.getCurrentSession().delete(realEstate);
    }

    @Transactional
    public Collection<RealEstate> retrieve() {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RealEstateBean.class);
        return criteria.list();
    }

    @Transactional
    public RealEstate retrieve(Serializable id) {
        return (RealEstate) sessionFactory.getCurrentSession().get(RealEstateBean.class, id);
    }

    
   

}
